package com.fs.controller;

import com.fs.bean.MonitorFalconTrend;
import com.fs.constant.DbInfoEnum;
import com.fs.domain.helper.MonitorFalconTrendHelper;
import com.fs.service.MonitorFalconService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by fengshuai on 17/6/16.
 */
@Controller
@RequestMapping("/trend")
public class DataTrendController {
    protected static Logger logger = Logger.getLogger(DataTrendController.class);

    @Autowired
    private MonitorFalconService monitorFalconService;

    @RequestMapping("/falcon/qps")
    @ResponseBody
    public ModelMap maxQpsTrend(@RequestParam(value = "type", defaultValue = "1") int type,
                                @RequestParam(value = "startDay") int startDay,
                                @RequestParam(value = "endDay") int endDay,
                                @RequestParam(value = "segment", defaultValue = "3") int segment,
                                HttpServletResponse response) {
        ModelMap map = new ModelMap();
        try {
            response.addHeader("Access-Control-Allow-Origin", "*");
            List<Integer> dbIds = DbInfoEnum.batchGetByType(type);
            List<MonitorFalconTrend> trends = this.monitorFalconService.batchGetMaxQpsByDayAndDbIdsV2(dbIds, startDay, endDay, segment);
            map.addAttribute("success", true);
            map.addAttribute("data", MonitorFalconTrendHelper.transToVo(trends));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            map.addAttribute("success", false).addAttribute("message", e.getMessage());
        }
        return map;
    }

}
